<!doctype html>
<html lang="en-US">
  <head>
    <link href="/assets/index.css" rel="stylesheet" type="text/css" />
    <script crossorigin="anonymous" src="/test-harness.js"></script>
    <script crossorigin="anonymous" src="/test-page-object.js"></script>
    <script crossorigin="anonymous" src="/__dist__/webchat-es5.js"></script>
  </head>
  <body>
    <main id="webchat"></main>
    <script type="importmap">
      {
        "imports": {
          "@testduet/wait-for": "https://unpkg.com/@testduet/wait-for@main/dist/wait-for.mjs"
        }
      }
    </script>
    <script type="module">
      import { waitFor } from '@testduet/wait-for';

      const typingAnimationBackgroundImage = `url('')`;

      run(async function () {
        const ponyfill = lolex.createClock();
        const { directLine, store } = testHelpers.createDirectLineEmulator({ ponyfill });

        const render = props =>
          WebChat.renderWebChat(
            {
              ...props,
              directLine,
              ponyfill,
              store
            },
            document.querySelector('main')
          );

        render({ styleOptions: { typingAnimationBackgroundImage, typingAnimationDuration: 1_000 } });

        await pageConditions.webChatRendered();

        // Wait for "Connecting..." message to dismiss
        ponyfill.tick(600);

        await pageConditions.uiConnected();

        // THEN: Should not show typing indicator initially.
        expect(pageElements.typingIndicator()).toBeFalsy();

        // WHEN: Send "typing 1" to the bot.
        await (
          await directLine.actPostActivity(() => pageObjects.sendMessageViaSendBox('typing 1', { waitForSend: false }))
        ).resolveAll();

        // WHEN: The bot send a message activity.
        await directLine.emulateIncomingActivity({
          from: { id: 'bot', role: 'bot' },
          id: crypto.randomUUID(),
          text: 'Typing indicator should go away after 5 seconds.',
          type: 'message'
        });

        // WHEN: The bot send a typing activity.
        await directLine.emulateIncomingActivity({
          from: { id: 'bot', role: 'bot' },
          id: crypto.randomUUID(),
          type: 'typing'
        });

        // THEN: Should receive a bot message.
        await waitFor(() => expect(pageElements.activities()).toHaveLength(2));

        // THEN: Should show typing indicator.
        await waitFor(() => expect(pageElements.typingIndicator()).toBeTruthy());

        // THEN: Should match snapshot.
        await host.snapshot('local');

        // WHEN: After 1 second.
        ponyfill.tick(1_000);

        // THEN: Should not show typing indicator.
        expect(pageElements.typingIndicator()).toBeFalsy();

        // THEN: Should match snapshot.
        await host.snapshot('local');

        // WHEN: Change "typingAnimationDuration" to 5 seconds.
        render({ styleOptions: { typingAnimationBackgroundImage, typingAnimationDuration: 5_000 } });

        // THEN: Should show typing indicator.
        expect(pageElements.typingIndicator()).toBeTruthy();

        // THEN: Should match snapshot.
        await host.snapshot('local');
      });
    </script>
  </body>
</html>
